package com.example.eshop_ssm.controller;

import com.example.eshop_ssm.annotation.CheckLogin;
import com.example.eshop_ssm.entity.TbOrderInfo;
import com.example.eshop_ssm.service.OrderManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/OrderManagerController")
public class OrderManagerController {

    @Autowired
    private OrderManagerService orderManagerService;

    /**
     * 跳转订单列表,并显示数据
     * @return
     */
    @CheckLogin
    @RequestMapping("/select")
    public ModelAndView forward(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(required = false)  String orderId,
                                @RequestParam(required = false)  String person,
                                @RequestParam(required = false)  String orderDate,
                                @RequestParam(required = false)  String status){
        ModelAndView modelAndView = new ModelAndView();
        HashMap<String, Object> orderLists = orderManagerService.selectOrders(page,orderId,person,orderDate,status);
        modelAndView.addObject("ordersList",orderLists);
        modelAndView.setViewName("ordermanager");
       // System.out.println(10/0);
        return modelAndView;
    }

    /**
     *删除单本
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteOrders")
    public Map<String,Object> deleteOrders(@RequestBody List<String> ids){
       // ModelAndView modelAndView = new ModelAndView();
        Integer integer = orderManagerService.deleteOrders(ids);
        HashMap<String, Object> deleteMap = new HashMap<>();
        deleteMap.put("effectRow",integer);
        return deleteMap;
    }

    /**
     * 跳转订单详情
     * @return
     */
    @RequestMapping("/reforwdSelectOrder/{orderId}")
    public ModelAndView reforwdSelectOrder(@PathVariable String orderId){
        ModelAndView modelAndView = new ModelAndView();
        TbOrderInfo tbOrderInfo = orderManagerService.orderDetail(orderId);//查询订单信息
        List<Map<String, Object>> maps = orderManagerService.selectGoods(orderId);//查询订单对应的商品信息
        modelAndView.addObject("order",tbOrderInfo);
        modelAndView.addObject("goods",maps);

        Double totalMoney=0.0;//总商品总金额
        //保留两位小数
        DecimalFormat df = new DecimalFormat("#.00");
        //计算总金额
        for (Map<String, Object> map : maps) {
            for (String key : map.keySet()) {
                if (key.equals("totalPrice")){
                    Double money = (Double) map.get("totalPrice");
                    totalMoney+= money ;
                }

            }
        }
        String totalMoneyFormat = df.format(totalMoney);
        modelAndView.addObject("totalMoney",totalMoneyFormat);
        //订单对应用户的操作信息
        List<Map<String, Object>> userOrderMap = orderManagerService.selectOrderUserDetail(orderId);
        modelAndView.addObject("userOrderMap",userOrderMap);
        modelAndView.setViewName("selectorder");
        return modelAndView;
    }

    /**
     * 批量取消或者删除
     * @return
     */
    @RequestMapping("/canclOrDelete")
    public Map canclOrDelete(String control,String orderIds){
        Integer integer = orderManagerService.canclOrDelete(control, orderIds);
        HashMap<Object, Object> effectRowMap = new HashMap<>();
        effectRowMap.put("effectRow",integer);
        return effectRowMap;
    }

    /**
     * 跳转发送订单
     * @return
     */
    @CheckLogin
    @RequestMapping("/reforwdOrderSend/{page}")
    public ModelAndView reforwdOrderSend(@PathVariable Integer page){
        ModelAndView modelAndView = new ModelAndView();
        HashMap<String, Object> sendOrderMap = orderManagerService.selectOrders(page, null, null, null, "status_send_1");
        modelAndView.addObject("sendOrderMap",sendOrderMap);
        modelAndView.setViewName("ordersend");
        return modelAndView;
    }


    /**
     * 跳转退单页面
     * @return
     */
    @RequestMapping("/reforawOrderReturns/{page}")
    public ModelAndView reforawOrderReturns(@PathVariable Integer page){
        ModelAndView modelAndView = new ModelAndView();
        HashMap<String, Object> backOrderMap = orderManagerService.selectOrders(page, null, null, null, "status_-1");
        modelAndView.addObject("backOrderMap",backOrderMap);
        modelAndView.setViewName("orderreturns");
        return modelAndView;
    }
}
